package com.example.springboot;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 用户控制器示例
 * 对比ASP.NET Core的Controller
 */
@RestController
@RequestMapping("/api/users")
public class UserController {
    
    // 模拟数据库
    private final Map<String, User> users = new HashMap<>();
    
    public UserController() {
        // 初始化一些测试数据
        users.put("1", new User("1", "张三", 25, "zhangsan@example.com"));
        users.put("2", new User("2", "李四", 30, "lisi@example.com"));
        users.put("3", new User("3", "王五", 28, "wangwu@example.com"));
    }
    
    /**
     * GET /api/users
     * 获取所有用户
     * 对比C#的[HttpGet]
     */
    @GetMapping
    public ResponseEntity<List<User>> getAllUsers() {
        return ResponseEntity.ok(new ArrayList<>(users.values()));
    }
    
    /**
     * GET /api/users/{id}
     * 根据ID获取用户
     * 对比C#的[HttpGet("{id}")]
     */
    @GetMapping("/{id}")
    public ResponseEntity<User> getUserById(@PathVariable String id) {
        User user = users.get(id);
        if (user != null) {
            return ResponseEntity.ok(user);
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    /**
     * POST /api/users
     * 创建新用户
     * 对比C#的[HttpPost]
     */
    @PostMapping
    public ResponseEntity<User> createUser(@RequestBody User user) {
        if (user.getId() == null || user.getId().trim().isEmpty()) {
            user.setId(UUID.randomUUID().toString());
        }
        
        users.put(user.getId(), user);
        return ResponseEntity.ok(user);
    }
    
    /**
     * PUT /api/users/{id}
     * 更新用户
     * 对比C#的[HttpPut("{id}")]
     */
    @PutMapping("/{id}")
    public ResponseEntity<User> updateUser(@PathVariable String id, @RequestBody User user) {
        if (!users.containsKey(id)) {
            return ResponseEntity.notFound().build();
        }
        
        user.setId(id);
        users.put(id, user);
        return ResponseEntity.ok(user);
    }
    
    /**
     * DELETE /api/users/{id}
     * 删除用户
     * 对比C#的[HttpDelete("{id}")]
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUser(@PathVariable String id) {
        if (users.remove(id) != null) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
    
    /**
     * GET /api/users/search?name={name}
     * 根据姓名搜索用户
     * 对比C#的[HttpGet("search")]
     */
    @GetMapping("/search")
    public ResponseEntity<List<User>> searchUsers(@RequestParam String name) {
        List<User> results = users.values().stream()
                .filter(user -> user.getName().contains(name))
                .toList();
        return ResponseEntity.ok(results);
    }
    
    /**
     * GET /api/users/count
     * 获取用户数量
     */
    @GetMapping("/count")
    public ResponseEntity<Map<String, Object>> getUserCount() {
        Map<String, Object> response = new HashMap<>();
        response.put("count", users.size());
        response.put("timestamp", new Date());
        return ResponseEntity.ok(response);
    }
}

/**
 * 用户实体类
 * 对比C#的模型类
 */
class User {
    private String id;
    private String name;
    private int age;
    private String email;
    
    // 默认构造函数 (Spring Boot需要)
    public User() {}
    
    public User(String id, String name, int age, String email) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.email = email;
    }
    
    // Getter和Setter方法
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", email='" + email + '\'' +
                '}';
    }
} 